package cn.xm.lingshi.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.xm.lingshi.pojo.Address;
import cn.xm.lingshi.pojo.YongHu;
import cn.xm.lingshi.service.YonghuService;
import cn.xm.lingshi.service.impl.YongHuServiceImpl;

public class AddressServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	
	public void destroy() {
		super.destroy(); 
		
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 接收请求数据
		request.setCharacterEncoding("UTF-8");
		YonghuService user=new YongHuServiceImpl();
		Address a=new Address();
		a.setAddress(request.getParameter("region")+request.getParameter("address"));
		a.setName(request.getParameter("name"));
		a.setPhone(request.getParameter("phone"));
		YongHu users=(YongHu)request.getSession().getAttribute("denglu");
		if(users!=null){
			a.setUserid(users.getUserId());
			boolean flag=user.addAddress(a);
			if(flag){
				out.print("<script>alert('添加成功！');history.back()</script>");
			}else{
				out.print("<script>alert('添加失败！');history.back()</script>");
			}
		}else{
			out.print("<script>alert('您未登录！');history.back()</script>");
		}
		
		
		out.flush();
		out.close();
	}

	
	public void init() throws ServletException {
		
	}

}
